package com.google.code.crawler;

import java.util.Iterator;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.crawler.core.CrawlerRegistry;
import com.google.code.crawler.tools.DocumentHandler;

public class CrawlerStandalone
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerStandalone.class);

    public static void main(String[] args) throws Exception
    {
        DocumentHandler documentHandler = CrawlerRegistry.getBean(DocumentHandler.class);

        Document doc = documentHandler.getDocument("http://thepiratebay.se");

        Elements elements = documentHandler.getElements("a", doc);

        if (elements != null && !elements.isEmpty())
        {
            for (Element link : elements)
            {

                if (link.attr("href") != null && !link.attr("href").startsWith("http"))
                {

                    Document fistLevelDoc = documentHandler.getDocument(link.attr("abs:href"));

                    logIframeUrls(fistLevelDoc);

                }

            }
        }

    }

    private static void logIframeUrls(Document document)
    {
        if (document != null)
        {

            Elements iframeElements = document.select("iframe");

            if (iframeElements != null && !iframeElements.isEmpty())
            {

                Iterator<Element> iframeElementIterator = iframeElements.iterator();

                while (iframeElementIterator.hasNext())
                {

                    LOGGER.info(iframeElementIterator.next().attr("src"));

                }

            }

        }
    }
}
